var menuMassLevelArray = new Array; //用来记录当前菜单层级状况的数组
//---------------------------------------------------------------------------------
var menuFunction = menuFunction || {};

menuFunction.menuReviewUp = function(){
	var eyeAfter = [PI/2,PI/2-0.001,eyeR,viewCenter[0],viewCenter[1],viewCenter[2]];
	view.changeView(eyeAfter,30);
	}
	
menuFunction.menuReviewDown = function(){
	var eyeAfter = [PI/2,0,eyeR,viewCenter[0],viewCenter[1],viewCenter[2]];
	view.changeView(eyeAfter,30);
	}	
	
menuFunction.menuReviewLeft = function(){
	var eyeAfter = [PI,0,eyeR,viewCenter[0],viewCenter[1],viewCenter[2]];
	view.changeView(eyeAfter,30);
	}
	
menuFunction.menuReviewRight = function(){
	var eyeAfter = [PI*3/4,PI/4,eyeR,viewCenter[0],viewCenter[1],viewCenter[2]];
	view.changeView(eyeAfter,30);
	}	
	
menuFunction.menuClearSelected = function(){
	creator.removeAllFrames();
	cubeSelectNow = [];
	}

menuFunction.changeCube = function(){
	worker.changeCube();
	}


menuFunction.menuEdit = function(){
			cubeSelect.push(cubeSelectNow[0][2]);	
			worker.changeOtherAlpha(cubeSelect[cubeSelect.length-1],0.1);
			creator.removeAllFrames();
			cubeSelectNow = [];
			view.resetView();
	}
//-----------------------打开/关闭画笔工具------------------------------------------------------------
menuFunction.colorPen = function(){
	if(!mouseButton["penMode"]){
			if(!totalColorUnitFrame){
			dialog.createTotalColorUnit();
			mouseButton["penMode"] = true;
				}
				else{
					dialog.moveDialogTo(totalColorUnitFrame,0,0);
					totalColorUnitFrame.addNewActiveNow();
					mouseButton["penMode"] = true;
					}
		mouseCursor = "NONE";
		if(!customizeCursor){
			dialog.createcustomizeCursor([
											defultValue.red,
											defultValue.green,
											defultValue.blue,
											1
											]);
			}else{
				customizeCursor.changeFrameAlpha([1]);
				}
					
		}else{
			mouseCursor = "DEFAULT";
			customizeCursor.changeFrameAlpha([0]);
			
			var checkNum = totalColorUnitFrame.checkActiveNum();
			if(checkNum != undefined){
				mouseButton["penMode"] = false;
				totalColorUnitFrame.delActiveNow();
				dialog.moveDialogTo(totalColorUnitFrame,-totalColorUnitFrame.width,0);
				dialog.tips.hidden();
				}
			}
	}
//---------------------------------------------------------
var menu = menu || {};

	
menu.createMenuMassBy = function(menuMassArray,menuDataArray){
	menuSelect.push(menuMassArray);
	if(!menuMassArray.transform){
		menuMassArray.transform = cubePack.createObject("Transform");
		}
	for(var n=0;n < menuDataArray.length;n++){
		if(menuDataArray[n][0]){
			menuMassArray[n] = new Array;
			menu.newOne.call(menuMassArray[n],menuDataArray[n].textureData,menuDataArray[n].size);
			menu.createMenuMassBy(menuMassArray[n],menuDataArray[n]);
			menuMassArray[n].action = menuDataArray[n].action;
			}
			else{
				menuMassArray[n] = new menu.newOne(menuDataArray[n].textureData,menuDataArray[n].size);
				menuMassArray[n].action = menuDataArray[n].action;
				}
		}
	menuSelect.pop();
	}

menu.init = function(){
	menu.initData();
	
	menuMaterial = o3djs.material.createMaterialFromFile(cubePack,
															 materialUrls[0],
															 menuViewInfo.zOrderedDrawList);
	menuMaterial.getParam("colorMult").value = [1, 1, 1, 1];
	menuMaterial.drawList = menuViewInfo.zOrderedDrawList;
	shape[3] = o3djs.primitives.createPlane(cubePack,
												menuMaterial,
												1,
												1,
												1,
												1,
												[[1, 0, 0, 0],
												[0, 0, 1, 0],
												[0,-1, 0, 0],
												[0, 0, 0, 1]]);
	
	
	//创建菜单数据
	menuMass[0] = new Array;
	menu.createMenuMassBy(menuMass[0],menuMassData[0]);
	menuMass[1] = new Array;
	menu.createMenuMassBy(menuMass[1],menuMassData[1]);
	menuMass[2] = new Array;
	menu.createMenuMassBy(menuMass[2],menuMassData[2]);
	menuMass[3] = new Array;
	menu.createMenuMassBy(menuMass[3],menuMassData[3]);
	menuMass[4] = new Array;
	menu.createMenuMassBy(menuMass[4],menuMassData[4]);
	}



menu.initData = function(){
	//初始化第0套菜单系统；
	
	menuMassDataCon = new Array;
	
	menuMassDataCon[0] = [,,,,,
						   [0,80,0.66,textures["menu_colorPen"],menuFunction.colorPen],
						   [7,80,0.66,textures["menu_ctrl_a"],worker.selectAllExist],
						   [6,80,0.66,textures["menu_r"],creator.insertCubeOri],
						   //[5,80,0.25,textures["menu_esc"],menuFunction.menuClearSelected],
						   [3,80,0.66,textures["menu_blank"],view.resetView],
						   [2,80,0.66,textures["menu_review"],function (){},
								[0,80,0.66,textures["menu_right"],menuFunction.menuReviewRight],
								[2,80,0.66,textures["menu_down"],menuFunction.menuReviewDown],
								[4,80,0.66,textures["menu_left"],menuFunction.menuReviewLeft],
								[6,80,0.66,textures["menu_up"],menuFunction.menuReviewUp]
								]//,
						   //[1,80,0.25,textures["menu_f"],view.resetCentreBySele]
						  ];
	menuMassData[0] = menu.createByArray(menuMassDataCon[0]);	
	
	menuMassDataCon[1] = [,,,,,
						   [0,80,0.66,textures["menu_colorPen"],menuFunction.colorPen],
						   [7,80,0.66,textures["menu_ctrl_a"],worker.selectAllExist],
						   [6,80,0.66,textures["menu_r"],creator.insertCubeOri],
						   [5,80,0.66,textures["menu_esc"],menuFunction.menuClearSelected],
						   [3,80,0.66,textures["menu_blank"],view.resetView],
						   [2,80,0.66,textures["menu_review"],function (){},
								[0,80,0.66,textures["menu_right"],menuFunction.menuReviewRight],
								[2,80,0.66,textures["menu_down"],menuFunction.menuReviewDown],
								[4,80,0.66,textures["menu_left"],menuFunction.menuReviewLeft],
								[6,80,0.66,textures["menu_up"],menuFunction.menuReviewUp]
								],
						   [1,80,0.66,textures["menu_f"],view.resetCentreBySele]
						  ];
	menuMassData[1] = menu.createByArray(menuMassDataCon[1]);	
	
	menuMassDataCon[2] = [,,,,,
						   [0,80,0.66,textures["menu_c"],menuFunction.changeCube],
						   [7,80,0.66,textures["menu_g"],worker.packCube],
						   [6,80,0.66,textures["menu_e"],menuFunction.menuEdit],
						   [5,80,0.66,textures["menu_ctrl_g"],worker.unPackCube],
						   [4,80,0.66,textures["menu_del"],worker.removeCubes]//,
						   //[2,80,0.25,textures["menu_m"],worker.openCloseMoveMode]
						  ];
	menuMassData[2] = menu.createByArray(menuMassDataCon[2]);
	
	menuMassDataCon[3] = [,,,,,
						   [0,80,0.66,textures["menu_c"],menuFunction.changeCube],
						   [7,80,0.66,textures["menu_g"],worker.packCube],
						   //[6,80,0.25,textures["menu_e"],menuFunction.menuEdit],
						   //[5,80,0.25,textures["menu_ctrl_g"],worker.unPackCube],
						   [4,80,0.66,textures["menu_del"],worker.removeCubes],
						   [2,80,0.66,textures["menu_m"],worker.openCloseMoveMode]
						  ];
	menuMassData[3] = menu.createByArray(menuMassDataCon[3]);
	
	menuMassDataCon[4] = [,,,,,
						   [0,80,0.66,textures["menu_c"],menuFunction.changeCube],
						   [7,80,0.66,textures["menu_g"],worker.packCube],
						   [6,80,0.66,textures["menu_e"],menuFunction.menuEdit],
						   [5,80,0.66,textures["menu_ctrl_g"],worker.unPackCube],
						   [4,80,0.66,textures["menu_del"],worker.removeCubes],
						   [2,80,0.66,textures["menu_m"],worker.openCloseMoveMode]
						  ];
	menuMassData[4] = menu.createByArray(menuMassDataCon[4]);
	
	}	
	
menu.createByArray = function(menuArray,menuDataArray,menuDataParent){
		if(menuArray[5]){
			menuDataArray = new Array;
			for(var n=5;n < menuArray.length;n++){
				menuDataArray[n-5] = new Object;
				menuDataArray[n-5] = menu.createByArray(menuArray[n],menuDataArray[n-5],menuDataArray);
				menuDataArray[n-5].angle = menuArray[n][0]*PI/4;
				menuDataArray[n-5].r = menuArray[n][1];
				menuDataArray[n-5].size = menuArray[n][2];
				menuDataArray[n-5].parent = menuDataArray;
				menuDataArray[n-5].textureData = menuArray[n][3];
				menuDataArray[n-5].action = menuArray[n][4];
				if(n == menuArray.length-1){
					return menuDataArray
					}
				}
			}
			else{
				menuDataArray.angle = menuArray[0]*PI/4;
				menuDataArray.r = menuArray[1];
				menuDataArray.size = menuArray[2];
				menuDataArray.parent = menuDataParent;
				menuDataArray.textureData = menuArray[3];
				menuDataArray.action = menuArray[4];
				return menuDataArray
				}
	}

menu._menuOrder = function(menuNow,menuData,menuNo){
	var menuCenterX = eyeMoveDelta[2];
	var menuCenterY = eyeMoveDelta[3];
	if (menuNow.parent){
		//有上级菜单且有子级菜单。
		if (menuNow[0]){
			var menuParent = menuNow.parent; 
			clearInterval(moveTime);
			var tStep = 20;
			var step = 0;
			var menuOffset = new Array;
			menuOffset['x'] = [];
			menuOffset['y'] = [];
			for (var n = 0;n < menuNow.length;n++){
				var menuOffsetX = menuData[n].r * Math.cos(menuData[n].angle);
				var menuOffsetY = menuData[n].r * Math.sin(menuData[n].angle);
				menuOffset['x'].push(menuOffsetX);
				menuOffset['y'].push(menuOffsetY);
				menuNow[n].centerX = menuOffsetX + menuNow.centerX;   
				menuNow[n].centerY = menuOffsetY + menuNow.centerY;
				menuNow[n].transform.identity();
				}
				
			moveTime = setInterval(function(){menu._moveEffect(menuNow,menuData,tStep,step,menuOffset);step++;},10);
			
			for(var menuUpN = 0 ;menuUpN < menuParent.length;menuUpN++){
				if (menuParent[menuUpN]!=menuNow){
				menuParent[menuUpN].moveTo(0,0); 
				menuParent[menuUpN].centerX = menuParent.centerX;  
				menuParent[menuUpN].centerY = menuParent.centerX;
				menuParent[menuUpN].changeAlpha(0);
					}
				}
		}//有上级菜单，无子级菜单，执行相应的函数
			else{
				menuData.action();
				menuMass.transform.identity();
				menuMassLevelArray = [];
				menu.formatAlpha(menuMass[menuNo]);
				menuMass[menuNo].transform.parent = null;
				menuMass[menuNo].centerX = menuMass[menuNo].centerY = undefined;//防止菜单的跳动
				}
		}else{ 
				if (menuNow.centerX == undefined){
				menuNow.transform.identity();
				menuNow.transform.translate(menuCenterX,menuCenterY,0);
			    menuNow.centerX = menuCenterX;
				menuNow.centerY = menuCenterY;
				}
				clearInterval(moveTime);
				var tStep = 30;
				var step = 0;
				var menuOffset = new Array;
				menuOffset['x'] = [];
				menuOffset['y'] = [];
				for (var n = 0;n < menuNow.length;n++){
					var menuOffsetX = menuData[n].r * Math.cos(menuData[n].angle);
					var menuOffsetY = menuData[n].r * Math.sin(menuData[n].angle);
					menuOffset['x'].push(Math.round(menuOffsetX));
					menuOffset['y'].push(Math.round(menuOffsetY));
					menuNow[n].centerX = menuOffsetX + menuNow.centerX;   
					menuNow[n].centerY = menuOffsetY + menuNow.centerY;
					menuNow[n].transform.identity();
					}
			moveTime = setInterval(function(){menu._moveEffect(menuNow,menuData,tStep,step,menuOffset);step++;},10);
			}
	};

menu.cancel = function(){
	if (menuMassLevelArray.length!=0){
			menu.formatAlpha(menuMass[menuMassLevelArray[0]]);
			menuMass[menuMassLevelArray[0]].transform.parent = null;
			menuMass[menuMassLevelArray[0]].centerX = menuMass[menuMassLevelArray[0]].centerY = undefined;				
			menuMassLevelArray = [];
		}
	}

menu.active = function(E,menuNo){
	if (menuMassLevelArray.length == 0){
		menuMassLevelArray.push(menuNo); 
		menu.formatAlpha(menuMass[menuNo]);
		menu._menuOrder(menuMass[menuNo],menuMassData[menuNo],menuNo);
		}
		else{
			var menuLevelMass = menu._levelCallBack(menuMass,menuMassLevelArray);
			var menuLevelMassData = menu._levelCallBack(menuMassData,menuMassLevelArray);
			var sortDistence = new Array;
			var sortZindex = new Array;
			var breakNotYet = true;
			for (var n=0;n < menuLevelMass.length;n++){
					var responseDistance = 
					Math.sqrt(Math.pow((menuLevelMass[n].centerX-E.x),2)+Math.pow((menuLevelMass[n].centerY-E.y),2));
					
					menu._scaleEffect(responseDistance,menuLevelMass[n]);
					
					sortDistence.push([responseDistance,n]);
					sortZindex.push(menuLevelMass[n].z_index);
					if (responseDistance < 32){
						menuMassLevelArray.push(n);
						menu.formatAlpha(menuMass[menuNo]);
						menu._menuOrder(menuLevelMass[n],menuLevelMassData[n],menuNo);
						breakNotYet = false;
						break;
						}
				}
				if(breakNotYet){menu._sortZindexByDis(sortDistence,sortZindex);}
				}
	}
	
//levelArray为全局变量 ，存储menu的级数。
menu._levelCallBack = function(mass,levelArray){
	var levelMass = mass;
		for (var n = 0;n < levelArray.length;n++){
		levelMass = levelMass[levelArray[n]];
			}
	return levelMass;
}

//放大缩小效果

menu._scaleEffect = function(distence,menuThis){
	if (distence >= 32){
	menuThis.scale(715.5/distence/Math.pow(distence,0.5)*menuThis.size);
	}
	}

//渐变弹出菜单的效果
menu._moveEffect = function(menuBoss,menuData,tStep,step,menuOffset){
	var shareStep;
	for (var an = 1;an < tStep;an++){
		if ((tStep-an)%(2*(menuBoss.length-1)) == 0){
			shareStep = an;
			break;
			}
		}
	var gapStep =(tStep-shareStep)/(2*(menuBoss.length-1));
	
	
	
	for (var n=0;n < menuBoss.length;n++){
		var startStep = n*gapStep;
		var endStep = tStep - (menuBoss.length-n-1)*gapStep-1;
		var afterStep = endStep - startStep +1;
		
		if (step>=startStep && step<=endStep){
		menuBoss[n].moveTo(menuOffset['x'][n]/Math.sin(0.7*PI)*Math.sin((step-startStep+1)/afterStep*PI*0.7),
						   menuOffset['y'][n]/Math.sin(0.7*PI)*Math.sin((step-startStep+1)/afterStep*PI*0.7),
									  0);
			
		menuBoss[n].changeAlpha(Math.sin((step-startStep+1)/afterStep*PI/2));
			}
		}
	if (step>=tStep){
		clearInterval(moveTime);
		moveTime = null;
		}
	}

menu._sortZindexByDis = function(sortDistence,sortZindex){
	sortDistence.sort(sortNumber);
	sortZindex.sort(sortFall);
	var nowMenuMass = menu._levelCallBack(menuMass,menuMassLevelArray);
	for (var i =0,j=nowMenuMass.length-1;i < nowMenuMass.length;i++,j--){
		nowMenuMass[sortDistence[i][1]].changeZ(sortZindex[j]);
		}
	}

	
	
menu.formatAlpha = function(menuArea){
	for (var n=0;n < menuArea.length;n++){
		if (menuArea[n][0]){
			menuArea[n].changeAlpha(0);
			menu.formatAlpha(menuArea[n]);
			}
			else {
				menuArea[n].changeAlpha(0);
				}
		}
	}	
	
menu.newOne = function(texture,size,x,y,alpha,z_index){
	this.x = x ? x : 0;
	this.y = y ? y : 0;
	this.z_index = z_index == undefined ? zBuffer++ : z_index;
	this.alpha = alpha ? alpha : 0;
	this.texture = texture;
	this.size = size;
	
	this.transform = cubePack.createObject("Transform");
	this.transform.parent = menuSelect[menuSelect.length-1].transform;
	this.parent = menuSelect[menuSelect.length-1];
	this.transform.translate(0,0,this.z_index);
	
	this.scaleTransform = cubePack.createObject("Transform");
	this.scaleTransform.parent = this.transform;
	
	this.sampler = cubePack.createObject('Sampler');
	this.sampler.addressModeU = cubeO3d.Sampler.CLAMP;
	this.sampler.addressModeV = cubeO3d.Sampler.CLAMP;
	
	this.paramSampler = this.scaleTransform.createParam('texSampler0',
													'ParamSampler');
	this.paramSampler.value = this.sampler;
	this.paramColorMult = this.scaleTransform.createParam('colorMult',
                                                    'ParamFloat4');
	this.paramColorMult.set(1, 1, 1, this.alpha);
	this.sampler.texture = texture;
	this.scaleTransform.addShape(shape[3]);	
	
	this.scaleTransform.scale(texture.width*size, -texture.height*size, 1);
	
	this.moveTo = function(x,y){
		this.transform.identity();
		this.transform.translate(x,y,this.z_index);
		this.x = x;
		this.y = y;
		}
	this.changeAlpha = function(alpha){
		this.paramColorMult.set(1, 1, 1, alpha);
		this.alpha = alpha;
		}
	this.scale = function(scaleAll){
		this.scaleTransform.identity();
		this.scaleTransform.scale(this.texture.width*scaleAll,-this.texture.width*scaleAll,1);
		
		}
	this.changeZ = function(z_index){
		this.z_index = z_index;
		this.transform.identity();
		this.transform.translate(this.x,this.y,this.z_index);
		}	
	}

